package com.hty.filemanager;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BookmarksActivity extends AppCompatActivity {
    ListView listView;
    List<String> list = new ArrayList<>();
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bookmarks);
        setTitle("书签");
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if(actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        listView = findViewById(R.id.listView_bookmark);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                String path = listView.getItemAtPosition(arg2).toString();
                Log.e(Thread.currentThread().getStackTrace()[2] + "", path);
                Intent intent = new Intent(BookmarksActivity.this, MainActivity.class);
                intent.putExtra("Path", path);
                startActivity(intent);
            }
        });
        listView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            @Override
            public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
                String title = ((TextView) info.targetView.findViewById(android.R.id.text1)).getText().toString();
                menu.setHeaderTitle(title);
                menu.add(0, 0, 0, "删除");
            }
        });
        refresh();
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        String title = ((TextView) menuInfo.targetView.findViewById(android.R.id.text1)).getText().toString();
        switch (item.getItemId()) {
            case 0:
                Toast.makeText(getApplicationContext(), "删除 " + title, Toast.LENGTH_SHORT).show();
                list.remove(title);
                adapter.notifyDataSetChanged();
                saveBookmarks();
                break;
        }
        return true;
    }

    void refresh() {
        String s = Utils.readFile("bookmarks.txt");
        //Log.e(Thread.currentThread().getStackTrace()[2] + "", s);
        String sa[] = s.split("\n");
        Collections.addAll(list, sa);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);
    }

    void saveBookmarks() {
        String s = "";
        for (int i=0; i<list.size(); i++){
            s += list.get(i);
            if (i < list.size() - 1)
                s += "\n";
        }
        Utils.writeFile("bookmarks.txt", s, MODE_PRIVATE);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int item_id = item.getItemId();
        switch (item_id) {
            case android.R.id.home:
                finish();
                return true;
        }
        return true;
    }

}